package com.ldy.streamMap;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.google.common.base.Joiner;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <br>
 * 版权: Copyright (c) 2011-2018<br>
 * 公司: 活力天汇<br>
 *
 * @author: 刘德咏<br>
 * @date: 2018-04-16<br>
 */
public class SteamMapTest {

    @Test
    public void testMatchCabin() {
        String flightList = "[\n" +
                "\t\t{\n" +
                "\t\t\t\"tripType\": \"OB\",\n" +
                "\t\t\t\"flightDate\": \"2018-05-17\",\n" +
                "\t\t\t\"flightNo\": \"MU5097\",\n" +
                "\t\t\t\"depCode\": \"SHA\",\n" +
                "\t\t\t\"arrCode\": \"TSA\",\n" +
                "\t\t\t\"cabin\": \"I\"\n" +
                "\t\t},\n" +
                "\t\t{\n" +
                "\t\t\t\"tripType\": \"IB\",\n" +
                "\t\t\t\"flightDate\": \"2018-05-20\",\n" +
                "\t\t\t\"flightNo\": \"MU5006\",\n" +
                "\t\t\t\"depCode\": \"TPE\",\n" +
                "\t\t\t\"arrCode\": \"PVG\",\n" +
                "\t\t\t\"cabin\": \"I\"\n" +
                "\t\t}\n" +
                "\t]";

        List<BookingFlight> bookingFlights = JSONObject.parseObject(flightList, new TypeReference<List<BookingFlight>>() {
        });

        Stream<String> flightKey = bookingFlights.stream().map(flight -> Joiner.on("$").join(flight.getCabin(), flight.getFlightNo(), flight.getDepCode(), flight.getArrCode()));
        List<String> collect = flightKey.collect(Collectors.toList());
        System.out.println(Arrays.toString(collect.toArray()));

        List<String> flightCabinKeys = bookingFlights.stream().map(flight -> Joiner.on("$").join(flight.getCabin(), flight.getFlightNo(), flight.getDepCode(), flight.getArrCode())).collect(Collectors.toList());
        for (String flightCabin : flightCabinKeys) {
            System.out.println(Arrays.toString(flightCabin.split("\\$", 2)));
        }

        Map<String, String> aaaa = bookingFlights.stream().collect(Collectors.toMap(flight -> Joiner.on("$").join(flight.getFlightNo(), flight.getDepCode(), flight.getArrCode()), v -> v.getCabin()));
        System.out.println(aaaa);


    }
}
